package com.fgs.common.widget.itemView;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fgs.common.R;
import com.fgs.common.utils.DateUtil;
import com.xuexiang.xui.widget.picker.widget.TimePickerView;
import com.xuexiang.xui.widget.picker.widget.builder.TimePickerBuilder;
import com.xuexiang.xui.widget.picker.widget.configure.TimePickerType;
import com.xuexiang.xui.widget.picker.widget.listener.OnTimeSelectListener;

import java.util.Calendar;
import java.util.Date;


/**
 * @author fgs
 * @date 2020/3/29
 * @description
 **/
public class DateItemView extends LinearLayout {
    private LinearLayout mItemLinearLayout;
    private ImageView mLeftImageView;
    private ImageView mRightImageView;
    private TextView mTipTextView;
    private TextView mTitleTextView;
    private TextView mContentTextView;
    private ImageView mArrowImageView;
    private String mTitle;
    private String mTimeTitle;
    private String mContent;
    private String mContentHint;
    private int mTitleTextColor;
    private int mContentTextColor;
    private int mContextHintColor;
    private boolean isShowArrow;
    private boolean isShowTittle;
    private boolean isShowTipText;
    private boolean isItemClickable;
    private TimePickerType timePickerType = TimePickerType.DEFAULT;
    private int dateType;
    private TimePickerView timePickerView;
    private OnSelectDateListener onSelectDateListener;
    private Date initDate;

    public DateItemView(Context context) {
        super(context);
        initView(context, null);
    }

    public DateItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
    }

    private void initView(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.DateItemView);


        int contentTextGravity;
        boolean isShowDefaultDate;

        try {
            mTitle = typedArray.getString(R.styleable.DateItemView_DateItemTitle);
            mTimeTitle = typedArray.getString(R.styleable.DateItemView_DateItemTimeTitle);
            mContent = typedArray.getString(R.styleable.DateItemView_DateItemContent);
            mContentHint = typedArray.getString(R.styleable.DateItemView_DateItemContentHint);
            mTitleTextColor = typedArray.getColor(R.styleable.DateItemView_DateItemTitleTextColor, context.getResources().getColor(R.color.colorTextBlack));
            mContentTextColor = typedArray.getColor(R.styleable.DateItemView_DateItemContentTextColor, context.getResources().getColor(R.color.colorTextPrimary));
            mContextHintColor = typedArray.getColor(R.styleable.DateItemView_DateItemContentHintTextColor, context.getResources().getColor(R.color.colorTextGray));

            isShowArrow = typedArray.getBoolean(R.styleable.DateItemView_DateItemIsShowArrow, true);
            isShowTittle = typedArray.getBoolean(R.styleable.DateItemView_DateItemIsTittleShow, true);
            isShowTipText = typedArray.getBoolean(R.styleable.DateItemView_DateItemIsShowTip, false);
            isItemClickable = typedArray.getBoolean(R.styleable.DateItemView_DateItemIsClickable, true);
            isShowDefaultDate = typedArray.getBoolean(R.styleable.DateItemView_DateItemIsShowDefaultDate, false);


            contentTextGravity = typedArray.getInt(R.styleable.DateItemView_DateItemContentGravity, 1);
            dateType = typedArray.getInt(R.styleable.DateItemView_DateItemDateType, 0);
        } finally {

            typedArray.recycle();
        }


        View view = LayoutInflater.from(context).inflate(R.layout.layout_date_item, null);
        LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        view.setLayoutParams(layoutParams);
        addView(view);

        mItemLinearLayout = view.findViewById(R.id.layout_date_item);
        mTipTextView = view.findViewById(R.id.layout_date_item_tipTextView);
        mTitleTextView = view.findViewById(R.id.layout_date_item_titleTextView);
        mContentTextView = view.findViewById(R.id.layout_date_item_contentTextView);
        mLeftImageView = view.findViewById(R.id.layout_date_item_leftImageView);
        mRightImageView = view.findViewById(R.id.layout_date_item_rightImageView);
        mArrowImageView = view.findViewById(R.id.layout_date_item_rightArrowImageView);

        setTitle(mTitle);
        setTimeTitle(mTimeTitle);
        setContent(mContent);
        setContentHint(mContentHint);
        setTitleTextColor(mTitleTextColor);
        setContentTextColor(mContentTextColor);
        setContextHintColor(mContextHintColor);
        setShowTitle(isShowTittle);
        setShowArrow(isShowArrow);
        setShowTip(isShowTipText);
        setItemClickable(isItemClickable);

        setContentTextGravity(contentTextGravity);
        setDateType(dateType);

        mItemLinearLayout.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isItemClickable) {
                    showTimePickerView();
                }
            }
        });

        if (isShowDefaultDate) {
            String defaultDate;
            if (dateType == 1) {
                defaultDate = DateUtil.getStandardTime(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss");
            } else {
                defaultDate = DateUtil.getStandardTime(System.currentTimeMillis(), "yyyy-MM-dd");
            }
            setContent(defaultDate);
        }
    }

    private void setTimeTitle(String timeTitle) {
        this.mTimeTitle = timeTitle;
        if (TextUtils.isEmpty(timeTitle)) {
            this.mTimeTitle = "请选择日期";
        }
    }

    private void showTimePickerView() {
        if (timePickerView == null) {



            timePickerView = new TimePickerBuilder(getContext(), new OnTimeSelectListener() {
                @Override
                public void onTimeSelected(Date date, View v) {

                    String time;
                    if (dateType == 1) {
                        time = DateUtil.getStandardTime(date.getTime(), "yyyy-MM-dd HH:mm:ss");
                    } else {
                        time = DateUtil.getStandardTime(date.getTime(), "yyyy-MM-dd");
                    }
                    setContent(time);

                    if (onSelectDateListener != null) {
                        onSelectDateListener.onSelectDate(time);
                    }
                }
            })
                    .setType(timePickerType)
                    .setTitleText(mTimeTitle)
                    .build();
        }
        if (initDate != null) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(initDate);
            timePickerView.setDate(calendar);
        }

        timePickerView.show();
    }

    private void setDateType(int dateType) {
        if (dateType == 1) {
            timePickerType = TimePickerType.ALL;
        } else {
            timePickerType = TimePickerType.DEFAULT;
        }
    }

    private void setShowTitle(boolean isShowTittle) {
        if (isShowTittle) {
            mTitleTextView.setVisibility(View.VISIBLE);
        } else {
            mTitleTextView.setVisibility(View.GONE);
        }
    }

    private void setContentTextGravity(int gravity) {
        switch (gravity) {
            case 0:
                mContentTextView.setGravity(Gravity.LEFT);
                break;
            case 1:
                mContentTextView.setGravity(Gravity.RIGHT);
                break;
            case 2:
                mContentTextView.setGravity(Gravity.CENTER);
                break;
        }
    }

    public void setItemClickable(boolean isClickable) {
        this.isItemClickable = isClickable;
        setClickable(isClickable);
        setEnabled(isClickable);
    }


    public String getTitle() {
        return mTitle;
    }

    public void setTitle(String title) {
        this.mTitle = title;
        mTitleTextView.setText(title);
    }

    public String getContent() {
        return mContent;
    }

    public void setContent(String content) {
        this.mContent = content;
        mContentTextView.setText(content);
        setTimePickerDate(content);
    }

    private void setTimePickerDate(String time) {
        if (!TextUtils.isEmpty(time)) {
            Date date;
            if (timePickerType == TimePickerType.ALL) {
                date = DateUtil.stringToDate(time, "yyyy-MM-dd HH:mm:ss");
            } else {
                date = DateUtil.stringToDate(time, "yyyy-MM-dd");
            }
            if (date != null) {
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(date);
                initDate = date;
            }

        }
    }


    public void setContentHint(String contentHint) {
        this.mContentHint = contentHint;
        if (!TextUtils.isEmpty(contentHint)) {

            mContentTextView.setHint(contentHint);
        }
    }


    public void setTitleTextColor(int titleTextColor) {
        this.mTitleTextColor = titleTextColor;
        mTitleTextView.setTextColor(titleTextColor);
    }

    public void setContentTextColor(int contentTextColor) {
        this.mContentTextColor = contentTextColor;
        mContentTextView.setTextColor(contentTextColor);
    }


    public void setContextHintColor(int contextHintColor) {
        this.mContextHintColor = contextHintColor;
        mContentTextView.setHintTextColor(contextHintColor);
    }


    public boolean isShowArrow() {
        return isShowArrow;
    }

    public void setShowArrow(boolean showArrow) {
        isShowArrow = showArrow;
        if (showArrow) {
            mArrowImageView.setVisibility(View.VISIBLE);
        } else {
            mArrowImageView.setVisibility(View.GONE);
        }
    }


    private void setShowTip(boolean isShowTipText) {

        this.isShowTipText = isShowTipText;
        if (isShowTipText) {
            mTipTextView.setVisibility(View.VISIBLE);
        } else {
            mTipTextView.setVisibility(View.INVISIBLE);
        }
    }


    public interface OnSelectDateListener {
        void onSelectDate(String date);
    }

    public void setOnSelectDateListener(OnSelectDateListener onSelectDateListener) {
        this.onSelectDateListener = onSelectDateListener;
    }
}
